﻿using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;

namespace Lottery.Client.SL.Controls
{
    public partial class Keyboard : UserControl,IKeyboard
    {
        public static readonly DependencyProperty TextProperty;
        public static Style SelectKey_1;
        public static Style SelectKey_2;
        public static Style NoneKey1;
        public static Style NoneKey2;
        static Keyboard()
        {
            Keyboard.TextProperty = DependencyProperty.RegisterAttached("KNameLable.Text",
                                                                        typeof (string), typeof (Keyboard),
                                                                        new PropertyMetadata(""));
        }
        public Keyboard()
        {
            InitializeComponent();
            SelectKey_1 = (Style) base.Resources["SelectKey_1"];
            SelectKey_2 = (Style) base.Resources["SelectKey_2"];
            NoneKey1 = (Style) base.Resources["NoneKey1"];
            NoneKey2 = (Style) base.Resources["NoneKey2"];
            Loaded += (SetText);
            // 1 2 3...........
            foreach (UIElement t in KeyNum.Children)
            {
                var canvas = (Canvas) t;
                canvas.MouseLeftButtonDown += (KeynumMouseLeftDown);
            }
            //全大小.........
            foreach (UIElement t in KeyCommand.Children)
            {
                var canvas2 = (Canvas) t;
                canvas2.MouseLeftButtonDown += (KeyCommandMouseLeftDown);
            }
            //Server
            // App.app_client.ClearKeyBoardChanged += new EventHandler(this.CX5BcwFPDQ);
        }

        public void SetText(object obj, RoutedEventArgs routedEventArgs)
        {
            KNameLable.Text = (GetText(this));
        }
        public static void SetText(object obj, object value)
        {
            DependencyObject dependencyObject = obj as DependencyObject;
            dependencyObject.SetValue(Keyboard.TextProperty, value);
        }
        public static string GetText(object obj)
        {
            var dependencyObject = obj as DependencyObject;
            object value = dependencyObject.GetValue(TextProperty);
            return value.ToString();
        }

        private void KeynumMouseLeftDown(object obj, MouseButtonEventArgs mouseButtonEventArgs)
        {
            var ballState = (Canvas) obj;
            SetBallState(ballState);
        }
        //\\?
        public void SetBallState(Canvas sender)
        {
            if (((Rectangle) sender.Children[1]).Style == SelectKey_1)
            {
                ChangeToNoneKey(sender);
            }
            else
            {
                ChangeToSelectedKey(sender);
            }
            CheckFull();
        }

        private void ChangeToNoneKey(Canvas canvas)
        {
            var rectangle = (Rectangle) canvas.Children[2];
            var rectangle2 = (Rectangle) canvas.Children[1];
            var textBlock = (TextBlock) canvas.Children[3];
            textBlock.Foreground = new SolidColorBrush(Colors.Black);
            rectangle2.Style = NoneKey1;
            rectangle.Style = NoneKey2;
        }

        private void ChangeToSelectedKey(Canvas canvas)
        {
            var rectangle = (Rectangle) canvas.Children[2];
            var rectangle2 = (Rectangle) canvas.Children[1];
            var textBlock = (TextBlock) canvas.Children[3];
            textBlock.Foreground = new SolidColorBrush(Colors.White);
            rectangle2.Style = SelectKey_1;
            rectangle.Style = SelectKey_2;
        }
        //\\?
        public void CheckFull()
        {
            DataCache.CurrentSchemeCache.InvestNum = this.GetLotteryInvestNum();
            DataCache.CurrentSchemeCache.InvestCount = this.GetLotteryNumber();
            //App.app_client.SetSelectInvestNum();
        }
        public bool GetBallState(Canvas sender)
        {
            bool result = !(((Rectangle)sender.Children[1]).Style == Keyboard.SelectKey_1);
            return result;
        }
        public string GetLotteryNumber()
        {
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < CulbViewMode.KeyList.Count; i++)
            {
                for (int j = 0; j < CulbViewMode.KeyList[i].KeyNum.Children.Count; j++)
                {
                    Canvas canvas = (Canvas)CulbViewMode.KeyList[i].KeyNum.Children[j];
                    if (!this.GetBallState(canvas))
                    {
                        stringBuilder.Append(((TextBlock)canvas.Children[3]).Text.Trim());
                    }
                }
                //111,222,333
                if (CulbViewMode.KeyList[i].KeyNum.Children.Count > 0)
                {
                    stringBuilder.Append(",");
                }
            }
            return stringBuilder.ToString().Remove(stringBuilder.ToString().Length - 1).Trim();
        }
        public int GetBallSelectedNum(Canvas cas)
        {
            int num = 0;
            for (int i = 0; i < cas.Children.Count; i++)
            {
                if (!this.GetBallState((Canvas)cas.Children[i]))
                {
                    num++;
                }
            }
            return num;
        }
        public int GetLotteryInvestNum()
        {
            int num = 1;
            string playTypeName = CulbViewMode.SelectGame.SelectGameMenu.SelectGameMode.PlayTypeName;
            if (playTypeName.Contains("复式"))
            {
                int ballSelectedNum = this.GetBallSelectedNum(CulbViewMode.KeyList[0].KeyNum);
                if (ballSelectedNum < 3)
                {
                    num = 0;
                }
                else
                {
                    if (ballSelectedNum >= 3)
                    {
                        num = ballSelectedNum * (ballSelectedNum - 1) * (ballSelectedNum - 2);
                    }
                }
            }
            else
            {
                if (playTypeName.Contains("二星"))
                {
                    int ballSelectedNum2 = this.GetBallSelectedNum(CulbViewMode.KeyList[0].KeyNum);
                    if (ballSelectedNum2 < 2)
                    {
                        num = 0;
                    }
                    else
                    {
                        num = ballSelectedNum2 * (ballSelectedNum2 - 1) * 3;
                    }
                }
                else
                {
                    if (playTypeName.Contains("二星"))
                    {
                        int ballSelectedNum3 = this.GetBallSelectedNum(CulbViewMode.KeyList[0].KeyNum);
                        if (ballSelectedNum3 < 2)
                        {
                            num = 0;
                        }
                        else
                        {
                            if (ballSelectedNum3 >= 2)
                            {
                                num = ballSelectedNum3 * (ballSelectedNum3 - 1);
                            }
                        }
                    }
                    else
                    {
                        if (playTypeName.Contains("二星"))
                        {
                            num = this.GetBallSelectedNum(CulbViewMode.KeyList[0].KeyNum);
                        }
                        else
                        {
                            if (playTypeName.Contains("复式") || playTypeName.Contains("复式"))
                            {
                                for (int i = 0; i < CulbViewMode.KeyList.Count; i++)
                                {
                                    num *= this.GetBallSelectedNum(CulbViewMode.KeyList[i].KeyNum);
                                }
                            }
                            else
                            {
                                num = 0;
                                for (int j = 0; j < CulbViewMode.KeyList.Count; j++)
                                {
                                    num += this.GetBallSelectedNum(CulbViewMode.KeyList[j].KeyNum);
                                }
                            }
                        }
                    }
                }
            }
            return num;
        }
        private void KeyCommandMouseLeftDown(object obj, MouseButtonEventArgs mouseButtonEventArgs)
        {
            var canvas = (Canvas) obj;
            SelecteKeyNums(canvas.Name);
        }

        private void SelecteKeyNums(string text)
        {
            for (int i = 0; i < KeyNum.Children.Count; i++)
            {
                ChangeToNoneKey((Canvas) KeyNum.Children[i]);
                if (text == "Q")
                {
                    //"全"
                    ChangeToSelectedKey((Canvas) KeyNum.Children[i]);
                }
                else
                {
                    if (text == "D" && i > 4)
                    {
                        //"大"
                        ChangeToSelectedKey((Canvas) KeyNum.Children[i]);
                    }
                    else
                    {
                        //"小"
                        if (text == "X" && i < 5)
                        {
                            ChangeToSelectedKey((Canvas) KeyNum.Children[i]);
                        }
                        else
                        {
                            if (text == "J" && i%2 != 0)
                            {
                                //"奇"
                                ChangeToSelectedKey((Canvas) KeyNum.Children[i]);
                            }
                            else
                            {
                                //"偶"
                                if (text == "O" && i%2 == 0)
                                {
                                    ChangeToSelectedKey((Canvas) KeyNum.Children[i]);
                                }
                            }
                        }
                    }
                }
            }
            CheckFull();
        }

        public void ShowKeyLable(Keyboard mKey)
        {
            this.KNameLable.Text = (GetText(mKey));
        }
    }
}